
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_status : false, //用户的登录状态（在storage中）登录为true，未登录为false
    user_image : 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    user_ID : "", //用户登录后的用户名
    
  },
  app_use(e){
    wx.navigateTo({
      url: "/package_my_content/pages/app_use/app_use",
    })
  },
  app_concerning(){
    wx.navigateTo({
      url: "/package_my_content/pages/app_concerning/app_concerning",
    })
  },
  app_settings(e){
    wx.navigateTo({
      url: "/package_my_content/pages/app_settings/app_settings",
    })
  },
  user_login(e){
    wx.navigateTo({
      url: "/package_my_content/pages/user_login/user_login",
    })
  },
  user_info(e){
    wx.navigateTo({
      url: "/package_my_content/pages/user_info/user_info",
    })
  },
  
  test_record_switch(e){
    wx.getStorage({
      key : "login_info",
      success (res){
        const db = wx.cloud.database()
        const todo = db.collection('record-test')
        var userID = res.data._id;
        todo.where({ id_user : userID }).count({
          success: function(res) {
            wx.navigateTo({
              url: `/package_my_content/pages/test_record/test_record?userID=${userID}&recordCount=${res.total}`,
            })
          },
          fail: console.error
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this

    wx.getStorage({
      key : 'login_status',
      success (res) {
        that.setData({ login_status : res.data, })

        if(res.data){
          wx.getStorage({
            key: 'login_info',
            success (res) {
              that.setData({ user_ID : res.data.account, })
            }
          })
          wx.getStorage({
            key: 'user_info',
            success (res) {
              that.setData({ user_image : res.data.avatar == "" ? "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0": res.data.avatar, })
            }
          })
        }
      },
      fail (res){
        that.setData({
          login_status : false,
          user_image : 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
        })
      }
    })

  },

})